/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import com.genia.toolbox.basics.editor.gui.AbstractGUI;
import com.genia.toolbox.basics.editor.gui.AbstractToolBar;
import com.genia.toolbox.basics.editor.model.document.Element;
import com.genia.toolbox.form.editor.FormEditorController;
import com.genia.toolbox.form.editor.gui.dialog.jspgeneration.JSPGenerationDialog;
import com.genia.toolbox.form.editor.message.Messages;
import com.genia.toolbox.form.editor.model.bean.FormInitialSettings;
import com.genia.toolbox.form.editor.model.document.impl.DocumentModel;
import com.genia.toolbox.form.editor.model.form.impl.ContainerFormModel;
import com.genia.toolbox.form.editor.model.form.impl.FormModel;
import com.genia.toolbox.form.editor.model.form.impl.FormType;
import com.genia.toolbox.web.portlet.description.ContainerPortletDescription.LayoutDirection;

/**
 * The main toolbar.
 */
@SuppressWarnings("serial")
public class FormEditorToolBar
    extends AbstractToolBar<FormEditorGUI, FormEditorController, DocumentModel, FormModel, FormInitialSettings>
{

  /**
   * The add item button.
   */
  private JButton buttonAddItem = null;

  /**
   * The add form button.
   */
  private JButton buttonAddForm = null;

  /**
   * The delete form button.
   */
  private JButton buttonDeleteForm = null;

  /**
   * The move up button.
   */
  private JButton buttonMoveUp = null;

  /**
   * The move down button.
   */
  private JButton buttonMoveDown = null;

  /**
   * The move right button.
   */
  private JButton buttonMoveRight = null;

  /**
   * The move left button.
   */
  private JButton buttonMoveLeft = null;

  /**
   * The JSP generation button.
   */
  private JButton buttonExportToJsp = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          the gui.
   */
  public FormEditorToolBar(FormEditorGUI gui)
  {
    super(gui);
  }



  /**
   * Initialise the specific commands of the tool bar.
   */
  @Override
  public void initialiseSpecificCommands()
  {

    URL url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/add.gif");
    this.buttonAddItem = new JButton(new ImageIcon(url));
    this.buttonAddItem.addActionListener(this);
    this.buttonAddItem.setToolTipText(this.getGui().getController().notifyTranslation(Messages.ACTION_ITEM_CREATE));
    this.add(this.buttonAddItem);

    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/form/editor/image/addForm.gif");
    this.buttonAddForm = new JButton(new ImageIcon(url));
    this.buttonAddForm.addActionListener(this);
    this.buttonAddForm.setToolTipText(this.getGui().getController().notifyTranslation(Messages.ACTION_FORM_CREATE));
    this.add(this.buttonAddForm);

    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/delete.gif");
    this.buttonDeleteForm = new JButton(new ImageIcon(url));
    this.buttonDeleteForm.addActionListener(this);
    this.buttonDeleteForm.setToolTipText(this.getGui().getController().notifyTranslation(Messages.ACTION_FORM_DELETE));
    this.add(this.buttonDeleteForm);

    this.addSeparator(new Dimension(AbstractGUI.TOOLBAR_SEPARATOR_WIDTH, 16));

    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/moveUp.gif");
    this.buttonMoveUp = new JButton(new ImageIcon(url));
    this.buttonMoveUp.addActionListener(this);
    this.buttonMoveUp.setToolTipText(this.getGui().getController().notifyTranslation(Messages.ACTION_FORM_UP));
    this.add(this.buttonMoveUp);

    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/moveDown.gif");
    this.buttonMoveDown = new JButton(new ImageIcon(url));
    this.buttonMoveDown.addActionListener(this);
    this.buttonMoveDown.setToolTipText(this.getGui().getController().notifyTranslation(Messages.ACTION_FORM_DOWN));
    this.add(this.buttonMoveDown);

    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/moveRight.gif");
    this.buttonMoveRight = new JButton(new ImageIcon(url));
    this.buttonMoveRight.addActionListener(this);
    this.buttonMoveRight.setToolTipText(this.getGui().getController().notifyTranslation(Messages.ACTION_FORM_RIGHT));
    this.add(this.buttonMoveRight);

    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/basics/editor/image/moveLeft.gif");
    this.buttonMoveLeft = new JButton(new ImageIcon(url));
    this.buttonMoveLeft.addActionListener(this);
    this.buttonMoveLeft.setToolTipText(this.getGui().getController().notifyTranslation(Messages.ACTION_FORM_LEFT));
    this.add(this.buttonMoveLeft);

    this.addSeparator(new Dimension(AbstractGUI.TOOLBAR_SEPARATOR_WIDTH, 16));

    url = FormEditorToolBar.class.getClassLoader().getResource("com/genia/toolbox/form/editor/image/eye.gif");
    this.buttonExportToJsp = new JButton(new ImageIcon(url));
    this.buttonExportToJsp.addActionListener(this);
    this.buttonExportToJsp.setToolTipText(this.getGui().getController().notifyTranslation(Messages.ACTION_JSP_EXPORT));
    this.add(this.buttonExportToJsp);

  }



  /**
   * Handle the action events.
   * 
   * @param e
   *          the action event.
   */
  @Override
  public void actionPerformed(ActionEvent e)
  {
    super.actionPerformed(e);
    Object source = e.getSource();
    if (source.equals(this.buttonAddItem)) {
      this.getGui().addSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.buttonAddForm)) {
      this.getGui().addSubForm(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.buttonDeleteForm)) {
      this.getGui().deleteSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.buttonMoveUp)) {
      this.getGui().moveUpOrRightSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.buttonMoveDown)) {
      this.getGui().moveDownOrLeftSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.buttonMoveRight)) {
      this.getGui().moveUpOrRightSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.buttonMoveLeft)) {
      this.getGui().moveDownOrLeftSubElement(this.getGui().getSelectedElement());
    }
    else if (source.equals(this.buttonExportToJsp)) {
      JSPGenerationDialog dialog = new JSPGenerationDialog(this.getGui());
      dialog.setVisible(true);
    }

  }



  /**
   * Refresh the access to the items.
   */
  @Override
  public void refresh()
  {
    super.refresh();
    FormModel model = this.getGui().getSelectedElement();
    if (model == null) {
      this.buttonAddItem.setEnabled(false);
      this.buttonAddForm.setEnabled(false);
      this.buttonDeleteForm.setEnabled(false);
      this.buttonMoveUp.setEnabled(false);
      this.buttonMoveDown.setEnabled(false);
      this.buttonMoveRight.setEnabled(false);
      this.buttonMoveLeft.setEnabled(false);
      this.getButtonCut().setEnabled(false);
      this.getButtonPaste().setEnabled(false);
    }
    else {
      if (model instanceof ContainerFormModel) {
        ContainerFormModel containerFormModel = (ContainerFormModel) model;
        int nbDisplayedSubForm = containerFormModel.getNbSubForms();
        if (nbDisplayedSubForm >= containerFormModel.getMaxNbSubForms()) {
          this.buttonAddItem.setEnabled(false);
          this.buttonAddForm.setEnabled(false);
        }
        else {
          this.buttonAddItem.setEnabled(true);
          this.buttonAddForm.setEnabled(true);
        }
      }
      else {
        this.buttonAddItem.setEnabled(false);
        this.buttonAddForm.setEnabled(false);
      }

      if (model.isSubElement()) {
        this.buttonDeleteForm.setEnabled(true);
        if (LayoutDirection.HORIZONTAL.equals(model.getParentDirection())) {
          this.buttonMoveUp.setEnabled(false);
          this.buttonMoveDown.setEnabled(false);
          this.buttonMoveRight.setEnabled(true);
          this.buttonMoveLeft.setEnabled(true);
        }
        else {
          this.buttonMoveUp.setEnabled(true);
          this.buttonMoveDown.setEnabled(true);
          this.buttonMoveRight.setEnabled(false);
          this.buttonMoveLeft.setEnabled(false);
        }
      }
      else {
        this.buttonDeleteForm.setEnabled(false);
        this.buttonMoveUp.setEnabled(false);
        this.buttonMoveDown.setEnabled(false);
        this.buttonMoveRight.setEnabled(false);
        this.buttonMoveLeft.setEnabled(false);
      }

      Element<?> element = this.getGui().getSelectedElement();
      if (element != null && element.getParentID() != null) {
        if (FormType.LABELED.equals(element.getType())) {
          this.getButtonCut().setEnabled(false);
        }
        else {
          this.getButtonCut().setEnabled(true);
        }
      }
      else {
        this.getButtonCut().setEnabled(false);
      }
      if (this.getGui().getController().notifyHasCutElement()) {
        if (element != null && element.isContainer()) {
          if (FormType.LABELED.equals(element.getType()) || FormType.TAB.equals(element.getType())) {
            this.getButtonPaste().setEnabled(false);
          }
          else {
            this.getButtonPaste().setEnabled(true);
          }
        }
        else {
          this.getButtonPaste().setEnabled(false);
        }
      }
      else {
        this.getButtonPaste().setEnabled(false);
      }
    }
  }
}
